package com.blobfish.kit;

import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONUtil;
import com.google.gson.Gson;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 文件工具
 */
public class FileKit {
    /**
     * 翻译结果json文件路径
     */
    public static final String FILE_PATH_RESULT = "E:\\workspace\\translateJson\\src\\main\\resources\\data\\result.json";
    /**
     * 所有的文件
     */
    public static final String[] FILE_PATH_LANG = {"G:\\hrqm\\基础版\\source\\src\\lang", "G:\\hrqm\\万宝矿产\\svn\\BeiJing-WanBaoKuangChan\\04_编码\\source\\vue-wbmining\\src\\lang"};
    /**
     * 要翻译的文件
     */
    public static final String FILE_PATH_TRANSLATE = "E:\\workspace\\translateJson\\src\\main\\resources\\file";

    /**
     * 返回所有的文件集合
     *
     * @return
     */
    public static List<File> listAllFile() {
        List<File> fileList = new ArrayList<>(50);
        for (String filePath :
                FILE_PATH_LANG) {
            fileList.addAll(cn.hutool.core.io.FileUtil.loopFiles(filePath, new FileFilter() {
                @Override
                public boolean accept(File pathname) {
                    return pathname.getName().endsWith("zh.js");
                }
            }));
        }
        return fileList;
    }

    /**
     * 返回待翻译的文件集合
     *
     * @return
     */
    public static List<File> listTranslateFile() {
        return cn.hutool.core.io.FileUtil.loopFiles(FILE_PATH_TRANSLATE, new FileFilter() {
            @Override
            public boolean accept(File pathname) {
                return pathname.getName().endsWith("zh.js");
            }
        });
    }

    /**
     * 获取结果map
     *
     * @return
     */
    public static Map<String, String> getResultMap() {
        Gson gson = new Gson();
        Map<String, String> resultMap;
        if (FileUtil.exist(FileKit.FILE_PATH_RESULT)) {
            String resultStr = FileUtil.readUtf8String(FileKit.FILE_PATH_RESULT);
            if (JSONUtil.isJson(resultStr)) {
                resultMap = gson.fromJson(resultStr, Map.class);
            } else {
                resultMap = new HashMap<>(2000);
            }
        } else {
            FileUtil.touch(FileKit.FILE_PATH_RESULT);
            resultMap = new HashMap<>(2000);
        }
        return resultMap;
    }
}

/**
 * public static void main(String[] args) {
 * List<File> fileList = cn.hutool.core.io.FileUtil.loopFiles("E:\\workspace\\translateJson\\src\\main\\resources\\lang", new FileFilter() {
 *
 * @Override public boolean accept(File pathname) {
 * return pathname.getName().endsWith("zh.js");
 * }
 * });
 * for (File file:
 * fileList) {
 * ArrayList<Callable<Integer>> callers = new ArrayList<Callable<Integer>>();
 * String langValueStr = cn.hutool.core.io.FileUtil.readUtf8String(file);
 * langValueStr = langValueStr.substring(15);
 * langValueStr = langValueStr.replaceAll("(?<!:)\\/\\/.*|\\/\\*(\\s|.)*?\\*\\/", "");
 * System.out.println(langValueStr);
 * JSONObject obj = JSONUtil.parseObj(langValueStr);
 * translate(obj, callers);
 * try {
 * pool.invokeAll(callers);
 * } catch (InterruptedException e) {
 * e.printStackTrace();
 * }
 * String jsonStr = "export default " + JSONUtil.formatJsonStr(JSONUtil.toJsonStr(obj));
 * cn.hutool.core.io.FileUtil.writeUtf8String(jsonStr, fileList.get(0).getAbsolutePath().replace("zh", "en"));
 * }
 * }
 * <p>
 * public static void translate(Object obj,ArrayList<Callable<Integer>> callers) {
 * if (obj instanceof JSONObject) {
 * final JSONObject tmpObj = (JSONObject) obj;
 * for (final String key : tmpObj.keySet()) {
 * if (tmpObj.get(key) instanceof String) {
 * callers.add(new Callable<Integer>() {
 * @Override public Integer call() throws Exception {
 * int retryNum = 3;
 * String resultStr = tmpObj.get(key).toString();
 * while (retryNum > 0) {
 * try {
 * resultStr = QuerierUtil.getInstance().querier(resultStr);
 * retryNum = 0;
 * } catch (Exception e) {
 * System.out.println(e.getStackTrace());
 * --retryNum;
 * }
 * }
 * tmpObj.put(key, resultStr);
 * return null;
 * }
 * });
 * } else {
 * translate(tmpObj.get(key), callers);
 * }
 * }
 * } else if (obj instanceof JSONArray) {
 * JSONArray tmpArray = (JSONArray) obj;
 * for (Object item : tmpArray) {
 * translate(item, callers);
 * }
 * }
 * }
 **/